import React, { Component } from 'react'
import classNames from 'classnames'
import '../css/index.css'

export default class Comment extends Component {
  state = {
    // 当前用户
    user: {
      name: '清风徐来',
      vip: true,
      avatar: 'https://static.youku.com/lvip/img/avatar/310/6.png',
    },
    // 评论列表
    comments: [
      {
        id: 100,
        name: '__RichMan',
        avatar: 'https://r1.ykimg.com/051000005BB36AF28B6EE4050F0E3BA6',
        content:
          '这阵容我喜欢😍靳东&闫妮，就这俩名字，我就知道是良心剧集...锁了🔒',
        time: '2021/10/12 10:10:23',
        vip: true,
        collect: false,
      },
      {
        id: 101,
        name: '糖蜜甜筒颖',
        avatar:
          'https://image.9xsecndns.cn/image/uicon/712b2bbec5b58d6066aff202c9402abc3370674052733b.jpg',
        content:
          '突围神仙阵容 人民的名义第三部来了 靳东陈晓闫妮秦岚等众多优秀演员实力派 守护人民的财产 再现国家企业发展历程',
        time: '2021/09/23 15:12:44',
        vip: false,
        collect: true,
      },
      {
        id: 102,
        name: '清风徐来',
        avatar: 'https://static.youku.com/lvip/img/avatar/310/6.png',
        content:
          '第一集看的有点费力，投入不了，闫妮不太适合啊，职场的人哪有那么多表情，一点职场的感觉都没有',
        time: '2021/07/01 00:30:51',
        vip: true,
        collect: false,
      },
    ],
    content: '',
  }

  changeContent = (e) => {
    const { value } = e.target
    if (value.length > 100) return
    this.setState({
      content: value.trim(),
    })
  }

  // 发布评论
  sendComment = () => {
    const { user, comments, content } = this.state
    if (!content.length) return alert('内容不能为空')
    const newArr = comments.filter((item) => {
      if (item.content === content) return alert('内容已存在')
      return item.content !== content
    })
    this.setState({
      comments: [
        {
          ...user,
          id: Math.random(),
          content,
          time: new Date().toLocaleString('chinese', { hour12: false }),
          collect: false,
        },
        ...newArr,
      ],
      content: '',
    })
  }

  // 删除按钮
  deleteClick = (e, id) => {
    const { comments } = this.state
    const newArr = comments.filter((item) => {
      return item.id !== id
    })
    this.setState({
      comments: [...newArr],
    })
  }

  // 收藏按钮
  collectClick = (e, id) => {
    const { comments } = this.state
    const newArr = comments.map((item) => {
      if (item.id === id) {
        item.collect = !item.collect
      }
      return item
    })
    this.setState({
      comments: [...newArr],
    })
  }

  render() {
    const { user, comments, content } = this.state
    return (
      <div className="comments">
        <h3 className="comm-head">评论</h3>
        <div className="comm-input">
          <textarea
            value={content}
            onChange={this.changeContent}
            placeholder="爱发评论的人，运气都很棒"
          ></textarea>
          <div className="foot">
            <div className="word">{content.length}/100</div>
            <div onClick={this.sendComment} className="btn">
              发表评论
            </div>
          </div>
        </div>
        <h3 className="comm-head">
          热门评论<sub>({comments.length})</sub>
        </h3>
        <ul className="comm-list">
          {comments.map((item) => {
            return (
              <li key={item.id} className="comm-item">
                <div className="avatar">
                  <img className="avatar" src={item.avatar} alt="" />
                </div>
                <div className="info">
                  <p className={classNames('name', item.vip ? 'vip' : '')}>
                    {item.name}
                    {item.vip ? (
                      <img
                        alt=""
                        src="https://gw.alicdn.com/tfs/TB1c5JFbGSs3KVjSZPiXXcsiVXa-48-48.png"
                      />
                    ) : null}
                  </p>
                  <p className="time">
                    {item.time}
                    <span
                      onClick={(e) => {
                        this.collectClick(e, item.id)
                      }}
                      className={`iconfont icon-collect${
                        item.collect ? '-sel' : ''
                      }`}
                    ></span>
                    {item.name === user.name ? (
                      <span
                        className="del"
                        onClick={(e) => {
                          this.deleteClick(e, item.id)
                        }}
                      >
                        删除
                      </span>
                    ) : null}
                  </p>
                  <p>{item.content}</p>
                </div>
              </li>
            )
          })}
        </ul>
      </div>
    )
  }
}
